package com.ruslan.magaz.service;


import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruslan.magaz.domain.Address;
import com.ruslan.magaz.domain.Customer;
import com.ruslan.magaz.domain.Role;
import com.ruslan.magaz.domain.User;
import com.ruslan.magaz.repository.UserDao;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
    private  UserDao userDao;

//	@Override
//	public void login(String user, String password) {
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	@Transactional
	public void addUser(User user) {
		userDao.addUser(user);
	}


	@Override
	@Transactional
	public void removeUser(User user) {
		userDao.removeUser(user);
	}

	@Override
	@Transactional
	public User getUserById(int id) {
		return userDao.getUserById(id);
	}

	@Override
	@Transactional
	public List<User> getAllUsers() {
		return userDao.getAllUsers();
	}
	@Override
	@Transactional
	public void editUser(User user) {
		userDao.editUser(user);
	}

	@Override
	@Transactional
	public void addRole(Role role) {
		userDao.addRole(role);
	}

	@Override
	@Transactional
	public void removeRole(Role role) {
		userDao.removeRole(role);
		
	}

	@Override
	@Transactional
	public Role getRoleById(int id) {
		return userDao.getRoleById(id);
	}

	@Override
	@Transactional
	public List<Role> getAllRoles() {
		return userDao.getAllRoles();
	}

//	@Override
//	public void setCustomer(User user, Customer customer) {
//		Customer old = user.getCustomer();
//		user.setCustomer(customer);
//		if (old == null){
//			userDao.addCustomer(customer);
//		} else {
//			userDao.updateCustomer(customer);
//		}
//	}

//	@Override
//	public Customer getCustomer(Customer customer) {
//		// TODO Auto-generated method stub
//		return null;
//	}

	@Override
	@Transactional
	public void addAddress(int userId, Address address) {
		userDao.addAddress(userId, address);
	}

	@Override
	@Transactional
	public void removeAddress(Address address) {
		userDao.removeAddress(address);
		
	}

	@Override
	public Address shippingAddressForCustomer(Customer customer) {
		Set<Address> addresses = customer.getAddresses();
		for (Address a: addresses){
			if(a.getAddressId() == customer.getShippingAddressId()){
				return a;
			}
		}
		return null;
	}

	@Override
	@Transactional
	public void editRole(Role role) {
		userDao.editRole(role);
		
	}


	@Override
	@Transactional
	public void addCustomer(int userId, Customer customer) {
		userDao.addCustomer(userId, customer);
	}


	@Override
	@Transactional
	public void removeCustomer(Customer customer) {
		userDao.removeCustomer(customer);
	}


	@Override
	@Transactional
	public void editCustomer(Customer customer) {
		userDao.editCustomer(customer);
	}


	@Override
	@Transactional
	public Customer getCustomerById(int id) {
		return userDao.getCustomerById(id);
	}

	@Override
	@Transactional
	public List<Customer> getAllCustomers() {
		return userDao.getAllCustomers();
	}


	@Override
	@Transactional
	public void editAddress(Address address) {
		userDao.editAddress(address);
	}


	@Override
	@Transactional
	public Address getAddressById(int id) {
		return userDao.getAddressById(id);
	}


	@Override
	@Transactional
	public List<Address> getAllAddressesForUser(int userId) {
		//TODO 
		return userDao.getAllAddressesForUser(userId);
	}


	@Override
	@Transactional
	public User login(String login, String password) {
		return userDao.login(login, password);
		
	}


	@Override
	public void updateDiscount(Customer customer) {
	}


	@Override
	public void favAddress(int userId, Address address) {
		userDao.favAddress(userId, address);
	}


	@Override
	@Transactional
	public List<User> getUserByName(String name) {
		return userDao.getUserByName(name);
	}


	@Override
	@Transactional
	public List<User> getUserByEmail(String email) {
		return userDao.getUserByEmail(email);
	}


	@Override
	@Transactional
	public Integer getFirstUserId() {
		return userDao.getFirstUserId();
	}

 
	@Override
	@Transactional
	public List<User> getAllUsersNotAdmin() {
		return userDao.getAllUsersNotAdmin();
	}



}


